import React, { useEffect, useState } from "react";
import {
  View,
  Text,
  StyleSheet,
  Platform,
  StatusBar,
  FlatList,
  Alert,
} from "react-native";
import NoData from "../../components/preAdmin/NoData";
import query from "../../api/query";
import AsyncStorage from "@react-native-async-storage/async-storage";
import UserCard from "../../components/preAdmin/UserCard";
import LoadingBar from "../../components/InterView/LoadingBar";
const ResearchManage = ({ navigation }) => {
  const [userList, setUserList] = useState([]);
  const [isLoading, setIsLoading] = useState(false);
  useEffect(() => {
    const fetchData = async () => {
      try {
        setIsLoading(true);
        const token = await AsyncStorage.getItem("token");

        const allUser = await query.get("/user/allUser", {
          headers: { Authorization: token },
        });
        setUserList(allUser.data.data);
      } catch (err) {
        console.log("初始化用户出错", err);
      }
      setIsLoading(false);
    };
    fetchData();
  }, []);

  const onPress = (item, navigation) => {
    navigation.navigate("ResearchExamine", {
      user: item,
    });
  };

  return (
    <View style={styles.container}>
      <LoadingBar visible={isLoading} />
      <FlatList
        style={{ padding: 10 }}
        ListEmptyComponent={<NoData />}
        keyExtractor={(item, index) => index.toString()}
        data={userList}
        ItemSeparatorComponent={() => (
          <View style={styles.itemSeparator}></View>
        )}
        renderItem={({ item, index }) => {
          return (
            <UserCard item={item} onPress={() => onPress(item, navigation)} />
          );
        }}
      />
    </View>
  );
};

var styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
  },
  itemSeparator: {
    width: "90%",
    height: 1,
    backgroundColor: "#eee",
    alignSelf: "center",
  },
});

export default ResearchManage;
